﻿using System;
using System.Data;
using System.Configuration;
using Dcms.BLL;
using Dcms.Model;
using Dcms.Common;
using System.Collections;
using System.Text;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;

public partial class Manage_Modle_CateGory_EditCate : System.Web.UI.Page
{
    public int CateID;
    private static readonly string IMAGE_PATH = ConfigurationManager.AppSettings["ImagePath"];

    Dcms_CateBLL cBll = new Dcms_CateBLL();

    protected void Page_Load(object sender, EventArgs e)
    {
        this.txtCate_Key.Attributes.Add("onchange", "this.value=myConvert(this.value);");
        CompetenceLib CL = new CompetenceLib();
        CL.IsLogin();
        try
        {
            if (!IsPostBack)
            {
                string strSql = " Model_UseFlag = 1 and (Model_Type<>'CateSYSTEM' or Model_Type<>'WEBSITEINFO' or Model_Type<>'SYSTEM') order by Model_Type ";
                Dcms_ModelBLL bllModel = new Dcms_ModelBLL();
                IList<Dcms_ModelModel> lDcms_Model = bllModel.GetModelList(strSql);
                foreach (Dcms_ModelModel etDcms_Model in lDcms_Model)
                {
                    this.ddlCate_Flag.Items.Add(new ListItem(etDcms_Model.Model_Name, etDcms_Model.Model_ID.ToString()));
                }

                btnSave.Enabled = false;
                btnDel.Visible = false;
                lab_Title.Text = "（请从左边选您所要操作的栏目或分类）";
                if (strCatID != "")
                {
                    CateID = Convert.ToInt32(strCatID);
                    btnSave.Enabled = true;

                    Dcms_CateModel cModel = new Dcms_CateModel();

                    Dcms_ModelBLL modelbll = new Dcms_ModelBLL();
                    
                
                    cModel = cBll.GetModelByID(CateID);
                    if (strAction == "edit")
                    {
                        lab_Title.Text = "（正在编辑“" + cModel.Cate_Name + "”类别）";
                        txb_CateName.Text = cModel.Cate_Name;
                        txb_CateOrder.Text = cModel.Cate_Order.ToString();
                        txtCate_Image.Text = cModel.Cate_Image;
                        txb_CateIntor.Text = cModel.Cate_Intro;
                        txb_CateUrl.Text = cModel.Cate_Url;
                        rad_State.SelectedValue = cModel.Cate_State.ToString();
                        rad_MemberRole.SelectedValue = cModel.Cate_MemberRole.ToString();
                        txb_AddTime.Text = cModel.Cate_AddTime.ToString("yyyy-MM-dd");
                        txtCate_Key.Text = cModel.Cate_Key;
                        btnDel.Visible = true;

                        this.ddlCate_Flag.SelectedValue = modelbll.GetModelByFalyOrder(cModel.Cate_Flag, cModel.Cate_ModelKeyId).Model_ID.ToString();
                        if (cModel.Cate_showtype == 0)
                        {
                            rblCate_showtype.Items[0].Selected = true;
                        }
                        else
                        {
                            rblCate_showtype.Items[1].Selected = true;
                        }

                    }
                    else
                    {
                        if (CateID != 0)
                        {
                            lab_Title.Text = "（正在新建“" + cModel.Cate_Name + "”的子级）";
                        }
                        else
                        {
                            lab_Title.Text = "（正在新建根级分类）";
                        }
                            txb_CateOrder.Text = "0";
                            txb_AddTime.Text = DateTime.Now.ToString("yyyy-MM-dd");
                            rad_State.SelectedValue = "1";
                            rblCate_showtype.Items[0].Selected = true;
                     
                     
                    }
                    if (Request.QueryString["SupID"] != null)
                    {
                        String strTemp = ("<script type=\"text/javascript\" language=\"javascript\">");
                        strTemp = strTemp + ("  parent.ExpandSubCategoryAgain(" + Request.QueryString["SupID"].ToString() + ",'edit');");
                        strTemp = strTemp + ("</script>");
                        lab_Script.Text = strTemp;
                    }
                    txb_CateName.Enabled = true;
                }
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_CateGory_EditCate.Page_Load", ex);
            throw ex;
        }
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        try
        {
            XmlDocument urls = new XmlDocument();
            urls.Load(Server.MapPath(@"../ModelField/FilePath.xml"));
            Dcms_ModelBLL ModelBll = new Dcms_ModelBLL();
            if (strAction == "edit")
            {
                Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
                //判断是否有更改功能模块


                ImageUploadUtil Iuu = new ImageUploadUtil();
                cModel.Cate_Name = txb_CateName.Text;
                cModel.Cate_Intro = txb_CateIntor.Text;
                cModel.Cate_State = rad_State.SelectedValue;
                cModel.Cate_MemberRole = rad_MemberRole.SelectedValue;
                cModel.Cate_Url = txb_CateUrl.Text;
                if (txb_CateOrder.Text == null)
                {
                    txb_CateOrder.Text = "0";
                }
                cModel.Cate_Order = Convert.ToInt32(txb_CateOrder.Text);
                cModel.Cate_AddTime = Convert.ToDateTime(txb_AddTime.Text);
                cModel.Cate_Key = txtCate_Key.Text.Trim();
                cModel.Cate_AllAction = "";
                cModel.Cate_Layer = 0;
                cModel.Cate_BackCate = 0;
                cModel.Cate_magstate = 0;
                cModel.Cate_showtype = Convert.ToInt32(this.rblCate_showtype.SelectedValue);              
                cModel.Cate_IsCapability = 0;
                cModel.Cate_auditing = 0;
                cModel.Cate_browse = 0;
                cModel.Cate_Guid = Guid.NewGuid().ToString();
                cModel.Cate_Image = txtCate_Image.Text.Trim();


                cModel.Cate_Flag = ModelBll.GetModelByID(Convert.ToInt32(ddlCate_Flag.SelectedItem.Value)).Model_Type;
                cModel.Cate_ModelKeyId = ModelBll.GetModelByID(Convert.ToInt32(ddlCate_Flag.SelectedItem.Value)).Model_Order;
                string showtype = "";
                if (cModel.Cate_showtype == 0)
                {
                    showtype = "edit";
                }
                else if (cModel.Cate_showtype == 1)
                {
                    showtype = "list";
                }

                foreach (XmlNode no in urls.GetElementsByTagName("model"))
                {


                    if (String.Compare(no.Attributes["name"].Value, cModel.Cate_Flag, true) == 0 && String.Compare(no.Attributes["PageType"].Value, showtype, true) == 0)
                    {
                        cModel.Cate_ViewURL = string.Format(no.Attributes["DestPath"].Value.Substring(9, no.Attributes["DestPath"].Value.Length - 9), cModel.Cate_ModelKeyId);
                        break;
                    }
                }
             
                cBll.Update(cModel);
                Response.Redirect("EditCate.aspx?CatID=" + cModel.Cate_ID + "&SupID=" + cModel.Cate_ParentID + "&action=edit", false);
            }
            else
            {
                Dcms_CateModel cModel;

                if (Convert.ToInt32(strCatID) != 0)
                {
                 cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
                }
                else
                {
                 cModel = new Dcms_CateModel();
                }



                //Dcms_CateModel cModel = new Dcms_CateModel();
                ImageUploadUtil Iuu = new ImageUploadUtil();
                cModel.Cate_Name = txb_CateName.Text;
                cModel.Cate_Intro = txb_CateIntor.Text;
                cModel.Cate_State = rad_State.SelectedValue;
                cModel.Cate_MemberRole = rad_MemberRole.SelectedValue;


                cModel.Cate_ParentID = Convert.ToInt32(strCatID);
                if (txb_CateOrder.Text == null)
                {
                    txb_CateOrder.Text = "0";
                }
                cModel.Cate_Order = Convert.ToInt32(txb_CateOrder.Text);
                cModel.Cate_AddTime = Convert.ToDateTime(txb_AddTime.Text);
                cModel.Cate_Key = txtCate_Key.Text.Trim();

                cModel.Cate_IsChild = 1;
                cModel.Cate_Flag = ModelBll.GetModelByID(Convert.ToInt32(ddlCate_Flag.SelectedItem.Value)).Model_Type;
                cModel.Cate_ModelKeyId = ModelBll.GetModelByID(Convert.ToInt32(ddlCate_Flag.SelectedItem.Value)).Model_Order;
                // cModel.Cate_AllAction = "";
                cModel.Cate_Layer = cModel.Cate_Layer + 1;
                cModel.Cate_BackCate = 0;
                cModel.Cate_magstate = 0;
                cModel.Cate_showtype = Convert.ToInt32(this.rblCate_showtype.SelectedValue);              
                cModel.Cate_IsCapability = 0;
                cModel.Cate_auditing = 0;
                cModel.Cate_browse = 0;
                cModel.Cate_Guid = Guid.NewGuid().ToString("N");
                cModel.Cate_Image = txtCate_Image.Text.Trim();
                if (Convert.ToInt32(strCatID) != 0)
                {
                Dcms_CateBLL parentCateBll = new Dcms_CateBLL();
                Dcms_CateModel parentCateModel = parentCateBll.GetModelByID(Convert.ToInt32(Request.QueryString["CatID"]));
               // cModel.Cate_ModelKeyId = parentCateModel.Cate_ModelKeyId;
                cModel.Cate_IDPath = parentCateModel.Cate_IDPath;
                }
             

                cModel.Cate_Url = txb_CateUrl.Text;

                string showtype = "";
                if (cModel.Cate_showtype == 0)
                {
                    showtype = "edit";
                }
                else if (cModel.Cate_showtype == 1)
                {
                    showtype = "list";
                }

                foreach (XmlNode no in urls.GetElementsByTagName("model"))
                {


                    if (String.Compare(no.Attributes["name"].Value, cModel.Cate_Flag, true) == 0 && String.Compare(no.Attributes["PageType"].Value, showtype, true) == 0)
                    {
                        cModel.Cate_ViewURL = string.Format(no.Attributes["DestPath"].Value.Substring(9, no.Attributes["DestPath"].Value.Length - 9), cModel.Cate_ModelKeyId);
                        break;
                    }
                }
                cModel.Cate_OpType = "EDIT";
                int newid=cBll.AddModel(cModel);
                if (Convert.ToInt32(strCatID) == 0 && Session["DCMSLANGUAGE"] != null)
                {
                    cModel.Cate_IDPath = "," + newid + ",";
                    cModel.Cate_AllAction = "Read|Add|Edit|Delete";
                    cModel.Cate_Lang= Session["DCMSLANGUAGE"].ToString();               
                    
                    cBll.Update(cModel);
                }

                cModel = cBll.GetDataListByPage("Select Top 1 * From Dcms_Cate Order By Cate_ID Desc", 1, 1)[0];
                Response.Redirect("EditCate.aspx?CatID=" + cModel.Cate_ID + "&SupID=" + cModel.Cate_ParentID + "&action=newCate", false);
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_CateGory_EditCate.btnSave_Click", ex);
            throw ex;
        }
    }

    protected void btnDel_Click(object sender, EventArgs e)
    {
        try
        {
            Dcms_CateBLL cBll = new Dcms_CateBLL();
            Dcms_CateModel cModel = cBll.GetModelByID(Convert.ToInt32(strCatID));
            if (cModel != null)
            {
                cBll.DeleteModelByID(cModel.Cate_ID);
                Response.Redirect("EditCate.aspx?CatID=" + cModel.Cate_ParentID + "&SupID=" + cModel.Cate_ParentID + "&action=add", false);
            }
        }
        catch (Exception ex)
        {
            Log.LogHandler.LogError("Manage_Modle_CateGory_EditCate.btnDel_Click", ex);
            throw ex;
        }
    }


    #region 属性
    private string strNewsID
    {
        get
        {
            return Request.QueryString["ID"] + "";
        }
    }

    private string strAction
    {
        get
        {
            return Request.QueryString["action"] + "";
        }
    }

    private string strCatID
    {
        get
        {
            return Request.QueryString["CatID"] + "";
        }
    }
    #endregion
}
